//
// Created by yyancy on 2019/10/27.
//

#include <asm/errno.h>
#include <errno.h>

#define MAXLINE 100


void err_sys(char *s) {
    fprintf(stderr, "%s\n", s);
    exit(1);
}


void
str_echo(int sockfd) {
    ssize_t n;
    char buf[MAXLINE];
    printf("begin echo service...\n");
    again:
    while ((n = read(sockfd, buf, MAXLINE - 1)) > 0) {
        buf[n] = 0;
        printf("received: %s, len is %ld\n", buf, n);
        write(sockfd, buf, n);
    }

    if (n < 0 && errno == EINTR)
        goto again;
    else if (n < 0)
        err_sys("str_echo: read error");
}
